```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>幂等性原理与实践</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background-color: #f9fafb;
            color: #1f2937;
            line-height: 1.6;
        }
        h1, h2, h3, h4 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.1);
        }
        .code-block {
            background-color: #1e293b;
            color: #f8fafc;
            border-radius: 0.375rem;
            font-family: monospace;
        }
        .solution-card {
            border-left: 4px solid #3b82f6;
        }
        .mermaid-container {
            background-color: white;
            border-radius: 0.5rem;
            padding: 1.5rem;
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
        }
        .drop-cap::first-letter {
            font-size: 3.5rem;
            font-weight: bold;
            float: left;
            line-height: 1;
            margin-right: 0.5rem;
            color: #3b82f6;
        }
    </style>
</head>
<body class="min-h-screen">
    <!-- Hero Section -->
    <header class="hero-gradient text-white py-20 px-4">
        <div class="max-w-6xl mx-auto">
            <div class="flex flex-col md:flex-row items-center">
                <div class="md:w-1/2 mb-10 md:mb-0">
                    <h1 class="text-4xl md:text-5xl font-bold mb-6">幂等性原理与实践</h1>
                    <p class="text-xl md:text-2xl mb-8 opacity-90">构建高可靠分布式系统的关键设计原则</p>
                    <div class="flex space-x-4">
                        <span class="inline-flex items-center px-3 py-1 rounded-full bg-white bg-opacity-20 text-sm">
                            <i class="fas fa-shield-alt mr-2"></i> 系统安全
                        </span>
                        <span class="inline-flex items-center px-3 py-1 rounded-full bg-white bg-opacity-20 text-sm">
                            <i class="fas fa-database mr-2"></i> 数据一致性
                        </span>
                    </div>
                </div>
                <div class="md:w-1/2">
                    <div class="mermaid-container">
                        <div class="mermaid">
                            graph TD
                                A[客户端] -->|1. 请求| B[服务端]
                                B -->|2. 处理请求| C{幂等性检查}
                                C -->|是| D[执行业务逻辑]
                                C -->|否| E[返回之前结果]
                                D --> F[返回结果]
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </header>

    <!-- Main Content -->
    <main class="max-w-6xl mx-auto px-4 py-12">
        <!-- Introduction Section -->
        <section class="mb-16">
            <div class="drop-cap bg-white rounded-xl shadow-md p-8">
                <h2 class="text-2xl font-bold mb-6 text-gray-800 border-b pb-2">背景与概念</h2>
                <p class="text-lg mb-4">在实际系统中，许多操作无论执行多少次，都应该产生相同的结果或效果。这种特性被称为幂等性，是构建可靠分布式系统的关键原则之一。</p>
                
                <div class="grid md:grid-cols-2 gap-6 mt-8">
                    <div class="bg-blue-50 rounded-lg p-6 border border-blue-100">
                        <h3 class="text-xl font-semibold mb-4 text-blue-800 flex items-center">
                            <i class="fas fa-exclamation-circle mr-2"></i> 典型案例
                        </h3>
                        <ul class="space-y-3">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-blue-500 mt-1 mr-2"></i>
                                <span>前端重复提交选中的数据，后台应该只产生对应这个数据的一个反应结果</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-blue-500 mt-1 mr-2"></i>
                                <span>发起付款请求应只扣款一次，即使网络重发或系统bug也应如此</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-blue-500 mt-1 mr-2"></i>
                                <span>同样的短信应该只发送一次给用户</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-blue-500 mt-1 mr-2"></i>
                                <span>创建业务订单，一次请求只创建一个订单</span>
                            </li>
                        </ul>
                    </div>
                    
                    <div class="bg-purple-50 rounded-lg p-6 border border-purple-100">
                        <h3 class="text-xl font-semibold mb-4 text-purple-800 flex items-center">
                            <i class="fas fa-lightbulb mr-2"></i> 幂等性概念
                        </h3>
                        <p class="mb-4">用户对于同一操作发起的一次请求或者多次请求的结果是一致的，不会因为多次点击而产生了副作用。</p>
                        <div class="bg-white p-4 rounded border">
                            <p class="text-sm italic text-gray-600">数学定义: f(f(x)) = f(x)</p>
                            <p class="text-sm mt-2">在HTTP协议中，GET、PUT、DELETE方法应该是幂等的，而POST通常不是。</p>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- CRUD Analysis Section -->
        <section class="mb-16">
            <div class="bg-white rounded-xl shadow-md p-8">
                <h2 class="text-2xl font-bold mb-6 text-gray-800 border-b pb-2">数据库操作的幂等性分析</h2>
                
                <div class="grid md:grid-cols-2 gap-8">
                    <!-- Add -->
                    <div class="solution-card bg-white p-6 rounded-lg shadow-sm">
                        <div class="flex items-center mb-4">
                            <div class="w-10 h-10 rounded-full bg-green-100 flex items-center justify-center mr-4">
                                <i class="fas fa-plus text-green-600"></i>
                            </div>
                            <h3 class="text-xl font-semibold">添加操作</h3>
                        </div>
                        
                        <div class="mb-4">
                            <h4 class="font-medium text-gray-700 mb-2">❌ 无幂等性</h4>
                            <div class="code-block p-4 mb-3 rounded">
                                <code>
                                    INSERT INTO product(name,type,price,tm)<br>
                                    VALUES ('商品1','电子',99.9,NOW())
                                </code>
                            </div>
                            <p class="text-sm text-gray-600">执行多次会新增多条记录</p>
                        </div>
                        
                        <div>
                            <h4 class="font-medium text-gray-700 mb-2">✅ 有幂等性</h4>
                            <div class="code-block p-4 mb-3 rounded">
                                <code>
                                    INSERT INTO product(orderId,name,type,price,tm)<br>
                                    VALUES ('ORD123','商品1','电子',99.9,NOW())
                                </code>
                            </div>
                            <p class="text-sm text-gray-600">利用主键唯一性保证只增加一条数据</p>
                        </div>
                    </div>
                    
                    <!-- Delete -->
                    <div class="solution-card bg-white p-6 rounded-lg shadow-sm">
                        <div class="flex items-center mb-4">
                            <div class="w-10 h-10 rounded-full bg-red-100 flex items-center justify-center mr-4">
                                <i class="fas fa-trash-alt text-red-600"></i>
                            </div>
                            <h3 class="text-xl font-semibold">删除操作</h3>
                        </div>
                        
                        <div class="mb-4">
                            <h4 class="font-medium text-gray-700 mb-2">✅ 有幂等性</h4>
                            <div class="code-block p-4 mb-3 rounded">
                                <code>
                                    DELETE FROM product WHERE id = 3
                                </code>
                            </div>
                            <p class="text-sm text-gray-600">执行多次效果相同</p>
                        </div>
                        
                        <div>
                            <h4 class="font-medium text-gray-700 mb-2">❌ 无幂等性</h4>
                            <div class="code-block p-4 mb-3 rounded">
                                <code>
                                    DELETE FROM product WHERE id > 23
                                </code>
                            </div>
                            <p class="text-sm text-gray-600">执行多次可能结果不同</p>
                        </div>
                    </div>
                    
                    <!-- Update -->
                    <div class="solution-card bg-white p-6 rounded-lg shadow-sm">
                        <div class="flex items-center mb-4">
                            <div class="w-10 h-10 rounded-full bg-yellow-100 flex items-center justify-center mr-4">
                                <i class="fas fa-edit text-yellow-600"></i>
                            </div>
                            <h3 class="text-xl font-semibold">更新操作</h3>
                        </div>
                        
                        <div class="mb-4">
                            <h4 class="font-medium text-gray-700 mb-2">✅ 有幂等性</h4>
                            <div class="code-block p-4 mb-3 rounded">
                                <code>
                                    UPDATE good SET stock=586 WHERE id=10
                                </code>
                            </div>
                        </div>
                        
                        <div>
                            <h4 class="font-medium text-gray-700 mb-2">❌ 无幂等性</h4>
                            <div class="code-block p-4 mb-3 rounded">
                                <code>
                                    UPDATE good SET stock=stock+10 WHERE id=10
                                </code>
                            </div>
                        </div>
                    </div>
                    
                    <!-- Select -->
                    <div class="solution-card bg-white p-6 rounded-lg shadow-sm">
                        <div class="flex items-center mb-4">
                            <div class="w-10 h-10 rounded-full bg-blue-100 flex items-center justify-center mr-4">
                                <i class="fas fa-search text-blue-600"></i>
                            </div>
                            <h3 class="text-xl font-semibold">查询操作</h3>
                        </div>
                        
                        <div class="code-block p-4 mb-3 rounded">
                            <code>
                                SELECT * FROM goods WHERE id = 10
                            </code>
                        </div>
                        <p class="text-gray-700">SELECT 查询天然具有幂等性，不会对数据产生副作用</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Solutions Section -->
        <section class="mb-16">
            <div class="bg-white rounded-xl shadow-md p-8">
                <h2 class="text-2xl font-bold mb-6 text-gray-800 border-b pb-2">幂等性解决方案</h2>
                <p class="text-lg mb-8">选择幂等性解决方案需要根据具体业务场景和技术架构来决定，以下是几种常见的实现方法：</p>
                
                <div class="grid md:grid-cols-2 gap-8">
                    <!-- Token Mechanism -->
                    <div class="card-hover bg-white p-6 rounded-lg shadow-sm border border-gray-100">
                        <div class="flex items-center mb-4">
                            <div class="w-12 h-12 rounded-full bg-indigo-100 flex items-center justify-center mr-4">
                                <i class="fas fa-key text-indigo-600 text-xl"></i>
                            </div>
                            <h3 class="text-xl font-semibold">Token 机制</h3>
                        </div>
                        
                        <div class="mermaid mb-4">
                            sequenceDiagram
                                participant C as Client
                                participant S as Server
                                participant R as Redis
                                C->>S: 1. 请求Token
                                S->>R: 生成Token并存储
                                S-->>C: 返回Token
                                C->>S: 2. 提交请求(携带Token)
                                S->>R: 验证并删除Token
                                alt Token有效
                                    S->>S: 执行业务逻辑
                                    S-->>C: 返回结果
                                else Token无效
                                    S-->>C: 拒绝重复请求
                                end
                        </div>
                        
                        <ul class="space-y-2 text-gray-700">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>数据提交前向服务申请token，token放到redis或jvm内存</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>提交后后台校验token，同时删除token</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>注意：redis要用删除操作来判断token，删除成功代表校验通过</span>
                            </li>
                        </ul>
                    </div>
                    
                    <!-- Other Solutions -->
                    <div class="space-y-6">
                        <!-- CAS -->
                        <div class="card-hover bg-white p-6 rounded-lg shadow-sm border border-gray-100">
                            <div class="flex items-center mb-3">
                                <div class="w-10 h-10 rounded-full bg-blue-100 flex items-center justify-center mr-4">
                                    <i class="fas fa-exchange-alt text-blue-600"></i>
                                </div>
                                <h3 class="text-xl font-semibold">CAS (Compare And Swap)</h3>
                            </div>
                            
                            <div class="code-block p-4 mb-3 rounded">
                                <code>
                                    UPDATE order SET status=1 <br>
                                    WHERE status=0 AND orderId="201251487987"
                                </code>
                            </div>
                            
                            <p class="text-gray-700">修改订单时携带状态，只有状态一致才能修改成功</p>
                        </div>
                        
                        <!-- Optimistic Lock -->
                        <div class="card-hover bg-white p-6 rounded-lg shadow-sm border border-gray-100">
                            <div class="flex items-center mb-3">
                                <div class="w-10 h-10 rounded-full bg-green-100 flex items-center justify-center mr-4">
                                    <i class="fas fa-lock text-green-600"></i>
                                </div>
                                <h3 class="text-xl font-semibold">乐观锁实现</h3>
                            </div>
                            
                            <div class="code-block p-4 mb-3 rounded">
                                <code>
                                    UPDATE t_goods SET count=count-1, version=version+1 <br>
                                    WHERE good_id=2 AND version=1
                                </code>
                            </div>
                            
                            <p class="text-gray-700">使用版本号解决CAS在高并发情况下的ABA问题</p>
                        </div>
                        
                        <!-- Other Methods -->
                        <div class="grid grid-cols-2 gap-4">
                            <div class="card-hover bg-white p-4 rounded-lg shadow-sm border border-gray-100">
                                <h4 class="font-semibold mb-2 flex items-center">
                                    <i class="fas fa-table mr-2 text-purple-500"></i> 防重表
                                </h4>
                                <p class="text-sm text-gray-600">插入时先将主键存入防重表，执行前先在防重表中判断</p>
                            </div>
                            
                            <div class="card-hover bg-white p-4 rounded-lg shadow-sm border border-gray-100">
                                <h4 class="font-semibold mb-2 flex items-center">
                                    <i class="fas fa-list-ol mr-2 text-orange-500"></i> 缓存队列
                                </h4>
                                <p class="text-sm text-gray-600">请求放入队列，异步处理并过滤重复消息</p>
                            </div>
                            
                            <div class="card-hover bg-white p-4 rounded-lg shadow-sm border border-gray-100 col-span-2">
                                <h4 class="font-semibold mb-2 flex items-center">
                                    <i class="fas fa-search-plus mr-2 text-teal-500"></i> Select + Insert
                                </h4>
                                <p class="text-sm text-gray-600">并发不高的系统中先查询关键数据判断是否已执行</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Decision Section -->
        <section>
            <div class="bg-white rounded-xl shadow-md p-8">
                <h2 class="text-2xl font-bold mb-6 text-gray-800 border-b pb-2">何时需要考虑幂等性</h2>
                
                <div class="grid md:grid-cols-2 gap-8">
                    <div class="bg-gray-50 p-6 rounded-lg">
                        <h3 class="text-xl font-semibold mb-4 text-gray-800 flex items-center">
                            <i class="fas fa-laptop-code mr-3 text-blue-500"></i> 技术层面
                        </h3>
                        <p class="mb-4">如果接口具备天然的幂等性(如查询操作、带有唯一约束的插入操作)则不需要特别考虑，否则需要设计幂等机制。</p>
                        
                        <div class="bg-white p-4 rounded border">
                            <p class="font-medium">需要特别关注的情况：</p>
                            <ul class="list-disc pl-5 mt-2 space-y-1">
                                <li>网络不稳定的环境</li>
                                <li>用户可能重复点击的场景</li>
                                <li>异步处理系统</li>
                                <li>分布式事务场景</li>
                            </ul>
                        </div>
                    </div>
                    
                    <div class="bg-gray-50 p-6 rounded-lg">
                        <h3 class="text-xl font-semibold mb-4 text-gray-800 flex items-center">
                            <i class="fas fa-briefcase mr-3 text-green-500"></i> 业务层面
                        </h3>
                        <p class="mb-4">如果出现幂等性问题对业务影响不大(如记录日志)，可以选择不解决；但对于关键业务(如支付、订单)，必须保证幂等性。</p>
                        
                        <div class="bg-white p-4 rounded border">
                            <p class="font-medium">关键业务场景：</p>
                            <ul class="list-disc pl-5 mt-2 space-y-1">
                                <li>金融交易系统</li>
                                <li>订单处理系统</li>
                                <li>库存管理系统</li>
                                <li>消息通知系统</li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            },
            sequenceDiagram: {
                diagramMarginX: 50,
                diagramMarginY: 10,
                actorMargin: 50,
                width: 300,
                height: 200
            }
        });
        
        // 简单的卡片悬停效果增强
        document.querySelectorAll('.card-hover').forEach(card => {
            card.addEventListener('mouseenter', function() {
                this.style.boxShadow = '0 10px 25px -5px rgba(0, 0, 0, 0.1)';
            });
            card.addEventListener('mouseleave', function() {
                this.style.boxShadow = '';
            });
        });
    </script>
</body>
</html>
```